/*
 * Name 		: Chang Hyun Park
 * Student ID	: 2007******
 * Major		: Computer Engineering
 * Year			: 3rd Year
 * Subject		: File Management
 * Filename		: inputmanager.c
 */

#include "header.h"

int get_buffer_size(void)
{
	int buffer_size=0;
	while(1)
	{
		printf("Please insert buffer size! Any value larger than 2! : ");
		scanf("%d",&buffer_size);

		if(buffer_size<3)
			printf("Error! Wrong value inserted! Try Again!\n");
		else
			break;
	}
	return buffer_size;
}

FILE* get_filename_input(int argc, char* argv[],char* filename)
{
	FILE* temp;
	if(argc==2)
	{
		printf("You inserted \"%s\" as filename.\n",argv[1]);
		sprintf(filename,"%s",argv[1]);
	}
	else
	{
		printf("You didn't specify filename! Running default \"gcc.trc\"\n");
		sprintf(filename,"%s","gcc.trc");
	}

	temp = fopen(filename, "r");
	if(temp==NULL)
	{
		printf("Error Opening File! Terminating\n");
		exit(1);
	}
	return temp;
}

int read_data(FILE* input, int** filedata, char* filename)
{
	int i=0, lines=0;
	char c;
	int *temp;


	while((c=fgetc(input))!=EOF)
		if(c == '\n')//Count number of lines!
			lines++;

	rewind(input);
	*filedata = (int*)malloc(sizeof(int)*lines);

	temp = *filedata;
	if(strcmp(filename, "ps.trc"))
	{//if filename isn't ps.trc
		for(i=0;i<lines;i++)
		{
			fscanf(input,"%d\t%c\n",&temp[i],&c);
		}
	}
	else
	{//If filename is ps.trc
		for(i=0;i<lines;i++)
		{
			fscanf(input,"%d\n",&temp[i],&c);
		}
	}

	return lines;
}


inline void clockTime( long * pSec, float * pUsec )
{
	struct timeval tv;
	gettimeofday (&tv, NULL);
	*pSec = tv.tv_sec;
	*pUsec = tv.tv_usec;
}

void print_header()
{
	printf("Algorithm\tBuffer Size\tHit rate\tTotal requests\tHit count\t"
			"Miss count\tElapsed Time!\n");
}
void print_stat(float percent, char* algorithm, int process, int hit, int miss,
																	float time)
{
	printf("%s\t\t%d\t\t%.2f%%\t\t%d\t\t%d\t\t%d\t\t%.6fs\n", algorithm,
			buffer_size, percent, process, hit , miss, time);
}
